package it.basilikos.core.model;

import it.basilikos.core.model.dao.IAgentDAO;
import it.basilikos.core.model.dao.IConfermentDAO;
import it.basilikos.core.model.dao.IProductConferredDAO;
import it.basilikos.core.model.dao.ISellDAO;
import it.basilikos.core.service.ProductConferredService;

import java.util.Date;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.google.common.collect.Lists;

public class App{



public static void main( String[] args )
{
	App a = new App();
	a.init();
	a.test();
	a.test2();
}
private ClassPathXmlApplicationContext context;

public void init()
{
	String home="WEB-INF/spring/"; 
		context = new ClassPathXmlApplicationContext(
				new String[]{
						home+"root-context.xml",
						home+"persistence-context.xml",
						home+"/appServlet/servlet-context.xml"  
				}
				);
		
		for( String all : context.getBeanDefinitionNames())
		{
			System.out.println("> "+all);
		}
	
}
public void test()
{
	 
	
	
	 
	IAgentDAO aDAO =  (IAgentDAO)context.getBean("agentDAO");
	Agent agent = aDAO.create(new Agent("pippo","password","x","x"));
	
	ISellDAO customerDAO =  (ISellDAO)context.getBean("sellDAO");
    Sell customer = new Sell(new Date(),28);
    customer.setAgent(agent);
    customerDAO.create(customer);
    
    
    IConfermentDAO cDAO = (IConfermentDAO)context.getBean("confermentDAO");
   
    
    Conferment conf =new Conferment(agent);    
//    conf.setProducts_conferred(set);
    conf = cDAO.create(conf);
    
//    List<ProductConferred> set = Lists.newArrayList();
    ProductConferred pc = new ProductConferred(conf);
    pc.setFruit("pere");
    ProductConferred pc2 = new ProductConferred(conf);
    pc2.setFruit("mele");
//    set.add(pc);
//    set.add(pc2);
    
    IProductConferredDAO cdo = (IProductConferredDAO)context.getBean("productConferredDAO");
    cdo.create(pc);
    cdo.create(pc2);
    
    
    
    
  
    

    List<Sell> customer1 = customerDAO.findAll();
    System.out.println(customer1);
    
    System.out.println("************");
    IProductConferredDAO pcDAO = (IProductConferredDAO)context.getBean("productConferredDAO");
    for(ProductConferred p:pcDAO.findAll())
    {
    	System.out.println(p+" ");
    	 
    }
}


public void test2()
{
	ProductConferredService service = (ProductConferredService)context.getBean("productConferredService");
	service.test();
}

}